package com.heima.media.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2022-06-28
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {


    @Autowired
    private MinIOService minIOService;

    @Override
    public ResponseResult saveWmMaterial(MultipartFile file) {

        User user = UserThreadLocalUtil.get();

        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        try {
            String url = minIOService.upload(file);

            WmMaterial wmMaterial = new WmMaterial();
            //wmMaterial.setId();                  //主键自增
            wmMaterial.setUserId(user.getUserId());//自媒体用户ID
            wmMaterial.setUrl(url);                //图片地址
            wmMaterial.setType(0);                 //0 图片   1 视频
            wmMaterial.setIsCollection(false);     //是否收藏
            wmMaterial.setCreatedTime(new Date()); //创建时间

            this.save(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult listByPage(WmMaterialDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();

        //是否收藏
        if (dto.getIsCollection() != null) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }

        //登录人
        queryWrapper.eq(WmMaterial::getUserId,user.getUserId());

        //排序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //分页
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        Page<WmMaterial> page1 = this.page(page, queryWrapper);

        return new PageResponseResult(dto.getPage(), dto.getSize(), page1.getTotal(), page1.getRecords());
    }


}
